﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_TraceInfo。
    /// </summary>
    public class House_TraceInfo
    {
        public House_TraceInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid TraceId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_TraceInfo where TraceId=@TraceId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, TraceId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_TraceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_TraceInfo(");
            strSql.Append("TraceId,TraceType,TraceContext,TractorId,Tractor,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,HouseId,HouseCode,HouseAddress,TraceTime,status,IsHide,OprPersonId,OprPerson,OprDepartId,OprDepart)");

            strSql.Append(" values (");
            strSql.Append("@TraceId,@TraceType,@TraceContext,@TractorId,@Tractor,@CityName,@CityId,@CompanyId,@CompanyName,@DepartId,@Depart,@HouseId,@HouseCode,@HouseAddress,@TraceTime,@status,@IsHide,@OprPersonId,@OprPerson,@OprDepartId,@OprDepart)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, model.TraceId);
            db.AddInParameter(dbCommand, "TraceType", DbType.Int32, model.TraceType);
            db.AddInParameter(dbCommand, "TraceContext", DbType.AnsiString, model.TraceContext);
            db.AddInParameter(dbCommand, "TractorId", DbType.Guid, model.TractorId);
            db.AddInParameter(dbCommand, "Tractor", DbType.AnsiString, model.Tractor);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "TraceTime", DbType.DateTime, model.TraceTime);
            db.AddInParameter(dbCommand, "status", DbType.Int32, model.status);
            db.AddInParameter(dbCommand, "IsHide", DbType.Boolean, model.IsHide);
            db.AddInParameter(dbCommand, "OprPersonId", DbType.Guid, model.OprPersonId);
            db.AddInParameter(dbCommand, "OprPerson", DbType.AnsiString, model.OprPerson);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_TraceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_TraceInfo set ");
            strSql.Append("TraceType=@TraceType,");
            strSql.Append("TraceContext=@TraceContext,");
            strSql.Append("TractorId=@TractorId,");
            strSql.Append("Tractor=@Tractor,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("Depart=@Depart,");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("HouseCode=@HouseCode,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("TraceTime=@TraceTime,");
            strSql.Append("status=@status,");
            strSql.Append("IsHide=@IsHide,");
            strSql.Append("OprPersonId=@OprPersonId,");
            strSql.Append("OprPerson=@OprPerson,");
            strSql.Append("OprDepartId=@OprDepartId,");
            strSql.Append("OprDepart=@OprDepart");
            strSql.Append(" where TraceId=@TraceId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, model.TraceId);
            db.AddInParameter(dbCommand, "TraceType", DbType.Int32, model.TraceType);
            db.AddInParameter(dbCommand, "TraceContext", DbType.AnsiString, model.TraceContext);
            db.AddInParameter(dbCommand, "TractorId", DbType.Guid, model.TractorId);
            db.AddInParameter(dbCommand, "Tractor", DbType.AnsiString, model.Tractor);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "TraceTime", DbType.DateTime, model.TraceTime);
            db.AddInParameter(dbCommand, "status", DbType.Int32, model.status);
            db.AddInParameter(dbCommand, "IsHide", DbType.Boolean, model.IsHide);
            db.AddInParameter(dbCommand, "OprPersonId", DbType.Guid, model.OprPersonId);
            db.AddInParameter(dbCommand, "OprPerson", DbType.AnsiString, model.OprPerson);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid TraceId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_TraceInfo ");
            strSql.Append(" where TraceId=@TraceId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, TraceId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_TraceInfo GetModel(Guid TraceId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,TraceType,TraceContext,TractorId,Tractor,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,HouseId,HouseCode,HouseAddress,TraceTime,status,IsHide,OprPersonId,OprPerson,OprDepartId,OprDepart from House_TraceInfo ");
            strSql.Append(" where TraceId=@TraceId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, TraceId);
            Chenxin.Lib.House.Entity.House_TraceInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,TraceType,TraceContext,TractorId,Tractor,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,HouseId,HouseCode,HouseAddress,TraceTime,status,IsHide,OprPersonId,OprPerson,OprDepartId,OprDepart ");
            strSql.Append(" FROM House_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_TraceInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,TraceType,TraceContext,TractorId,Tractor,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,HouseId,HouseCode,HouseAddress,TraceTime,status,IsHide,OprPersonId,OprPerson,OprDepartId,OprDepart ");
            strSql.Append(" FROM House_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_TraceInfo> list = new List<Chenxin.Lib.House.Entity.House_TraceInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        public static List<Chenxin.Lib.House.Entity.House_TraceInfo> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,TraceType,TraceContext,TractorId,Tractor,DepartId,Depart,HouseId,HouseCode,HouseAddress,TraceTime,status,IsHide ");
            strSql.Append(" FROM House_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order By " + strOrder);
            }

            List<Chenxin.Lib.House.Entity.House_TraceInfo> list = new List<Chenxin.Lib.House.Entity.House_TraceInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_TraceInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_TraceInfo model = new Chenxin.Lib.House.Entity.House_TraceInfo();
            object ojb;
            ojb = dataReader["TraceId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceId = new Guid(ojb.ToString());
            }
            ojb = dataReader["TraceType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceType = (int)ojb;
            }
            model.TraceContext = dataReader["TraceContext"].ToString();
            ojb = dataReader["TractorId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TractorId = new Guid(ojb.ToString());
            }
            model.Tractor = dataReader["Tractor"].ToString();
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.Depart = dataReader["Depart"].ToString();
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.HouseCode = dataReader["HouseCode"].ToString();
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            ojb = dataReader["TraceTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceTime = (DateTime)ojb;
            }
            ojb = dataReader["status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.status = (int)ojb;
            }
            ojb = dataReader["IsHide"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsHide = (bool)ojb;
            }
            ojb = dataReader["OprPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprPersonId = new Guid(ojb.ToString());
            }
            model.OprPerson = dataReader["OprPerson"].ToString();
            ojb = dataReader["OprDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprDepartId = new Guid(ojb.ToString());
            }
            model.OprDepart = dataReader["OprDepart"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

